package de.fzi.kasma.learner.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

public class EPTab {
	
	public StartGui startGUI;
	public TabItem tab;
	public Group group5;
	public Group group4;

	public EPTab(StartGui startGui, TabFolder parent, int style) {

		
		tab = new TabItem(parent, style);



		tab.setText ("Evolutionary Algorithm Settings");
		
		
		Group group = new Group (parent, SWT.SHADOW_NONE);
		GridLayout gridLayout = new GridLayout ();
		gridLayout.numColumns = 2;
		group.setLayout (gridLayout);
		
		/* The next group of parameters */
		group4 = new Group (group, SWT.SHADOW_NONE);
		group4.setLayout (gridLayout);
		group4.setText ("General parameters for the Evolutionary Algorithm");

		createParameterItem(group4, "verbosity", "0");
		createParameterItem(group4, "breedthreads", "1");
		createParameterItem(group4, "evalthreads", "1");
		createParameterItem(group4, "seed.0", "4357");
		
		createParameterItem(group4, "generations", "10");
		createParameterItem(group4, "quit-on-run-complete", "true");
		createParameterItem(group4, "checkpoint", "false");
		createParameterItem(group4, "checkpoint-modulo", "1");
		createParameterItem(group4, "stat.file", "$out.stat");
		createParameterItem(group4, "state", "ec.simple.SimpleEvolutionState");
		
		createParameterItem(group4, "pop", "ec.Population");
		createParameterItem(group4, "init", "de.fzi.kasma.learner.genetic.RLInitializer");
		createParameterItem(group4, "finish", "ec.simple.SimpleFinisher");
		createParameterItem(group4, "breed", "ec.simple.SimpleBreeder");
		createParameterItem(group4, "eval", "ec.coevolve.MultiPopCoevolutionaryEvaluator");
		createParameterItem(group4, "stat", "ec.simple.SimpleStatistics");
		createParameterItem(group4, "exch", "ec.simple.SimpleExchanger");
		
		/* The next group of parameters */
		group5 = new Group (group, SWT.SHADOW_OUT);
		group5.setLayout (gridLayout);
		group5.setText ("Populations Settings");

		createParameterItem(group5, "pop.subpops", "2");
		createParameterItem(group5, "eval.subpop.0.num-rand-ind", "2");
		createParameterItem(group5, "eval.subpop.0.num-elites", "1");
		createParameterItem(group5, "eval.subpop.0.num-ind", "0");
		createParameterItem(group5, "seval.subpop.0.select.size", "1");
		createParameterItem(group5, "eval.subpop.1.num-rand-ind", "2");
		createParameterItem(group5, "eval.subpop.1.num-elites", "1");
		createParameterItem(group5, "eval.subpop.1.num-ind", "0");
		createParameterItem(group5, "eval.subpop.1.select.size", "1");
		
		createParameterItem(group5, "pop.subpop.0.size", "4");
		createParameterItem(group5, "op.subpop.0.species.mutation-prob", "0.01");
		createParameterItem(group5, "pop.subpop.0.species.crossover-prob", "0.95");
		createParameterItem(group5, "pop.subpop.1.size", "4");
		createParameterItem(group5, "pop.subpop.1.species.mutation-prob", "0.95");
		createParameterItem(group5, "pop.subpop.1.species.crossover-prob", "1.0");
		createParameterItem(group5, "breed.elite.0", "1");
		createParameterItem(group5, "breed.elite.1", "1");
	   
		tab.setControl(group);
	}
	protected void createParameterItem(Composite parent, String label, String text){
		
	    Label lbl = new Label(parent, SWT.NORMAL);
	    lbl.setText(label);
		Text txt = new Text(parent, SWT.BORDER);
	    txt.setText(text);
	    txt.setEditable(true);
		
	}
	public String[] getParams() {
		
	      String[] params = new String[34];
	      
	      for(int i = 0, j = 0; i<17; i++, j+=2){
	    	  params[i] = ((Label) group4.getChildren()[j]).getText()+"="+((Text) group4.getChildren()[j+1]).getText();
	      }
	      for(int i = 17, j = 0; i<34; i++, j+=2){
	    	  params[i] = ((Label) group5.getChildren()[j]).getText()+"="+((Text) group5.getChildren()[j+1]).getText();
	      }
		      
			return params;
	}

}
